package com.rfid.afd.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.rfid.afd.activity.adapter.DeleteAdapter;
import com.rfid.afd.activity.adapter.SearchBookAdapter;
import com.rfid.afd.activity.listener.CheckRfidListener;
import com.rfid.afd.activity.view.ISearchView;
import com.rfid.afd.presenter.BasePresenter;
import com.rfid.afd.presenter.SearchPresenter;
import com.rfid.db.bean.Rfid;
import com.rfid.rls_psw.R;
import com.rfid.util.SearchManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchBookActivity extends BaseActivity<SearchPresenter> implements ISearchView ,Handler.Callback{


    @BindView(R.id.edit)
    EditText mEdit;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    SearchBookAdapter mAdapter;
    private List<Rfid> mRfids;
    private Handler mHandler;
    private List<Rfid> mSelects;
    private boolean mSelectAll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchbook_layout);
        ButterKnife.bind(this);
        mRfids = new ArrayList<>();
        mAdapter = new SearchBookAdapter(this,mRfids);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,RecyclerView.VERTICAL));
        LayoutAnimationController controller = new LayoutAnimationController(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
        mRecyclerView.setLayoutAnimation(controller);
        mSelects = SearchManager.getInstance().getSearchRfids();
        for(Rfid rfid : mSelects) {
            rfid.setChecked(true);
        }
        mRfids.addAll(mSelects);
        mHandler = new Handler(this);
        mEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = s.toString();
                if(TextUtils.isEmpty(text)) {
                    mHandler.removeMessages(0);
                    mRfids.clear();
                    mRfids.addAll(mSelects);
                    mAdapter.notifyDataSetChanged();
                    return;
                }
                mHandler.removeMessages(0);
                mHandler.sendMessageDelayed(Message.obtain(mHandler,0,text),300);
            }
        });

        mAdapter.setCheckRfidListener(new CheckRfidListener() {
            @Override
            public void onChecked(boolean check, Rfid rfid) {
                System.out.println("rfid = " + rfid + ",check = " + check);
                if(check) {
                    mSelects.add(rfid);
                } else {
                    mSelects.remove(rfid);
                }
            }
        });
    }

    @Override
    public SearchPresenter getPresenter() {
        return new SearchPresenter();
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void stopLoading() {
    }

    boolean scan;
    public void scan(View view) {
//        String text = mEdit.getText().toString();
//        if(TextUtils.isEmpty(text)) {
//            Toast.makeText(this,"请输入要查找的书名",Toast.LENGTH_SHORT).show();
//            return;
//        }
//        TextView textView = (TextView) view;
//        if(scan) {
//            mPresenter.stopScanRfid();
//            textView.setText("开始扫描");
//        } else {
//            mPresenter.startScanRfid();
//            textView.setText("停止扫描");
//        }
//        scan = !scan;
    }

//    public void delete(View view) {
//        mPresenter.delete(mRfids);
//    }

    public void quit(View view) {
//        for(Rfid rfid :mSelects) {
//            rfid.setChecked(false);
//        }
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        SearchManager.getInstance().save();
    }

    @Override
    public void finish() {
        super.finish();
        SearchManager.getInstance().save();
    }

    @Override
    public void onRfidsByBookName(List<Rfid> rfids) {
        System.out.println("onRfidsByBookName size = " + rfids.size());
        mRfids.clear();
        mRfids.addAll(rfids);
        mAdapter.notifyDataSetChanged();
    }


    @Override
    public boolean handleMessage(@NonNull Message msg) {
        mPresenter.queryRfidsByBookNameOrBarCode((String) msg.obj);
        return false;
    }


    public void select(View view) {
        TextView textView = (TextView) view;
        if(mSelectAll) {
            for(Rfid rfid :mRfids) {
                rfid.setChecked(false);
                if(mSelects.contains(rfid)) {
                    mSelects.remove(rfid);
                }
            }
            textView.setText("全选");
        } else {
            textView.setText("反选");
            for(Rfid rfid :mRfids) {
                rfid.setChecked(true);
                if(!mSelects.contains(rfid)) {
                    mSelects.add(rfid);
                }
            }
        }
        mSelectAll = !mSelectAll;
        mAdapter.notifyDataSetChanged();
    }

    public void confirm(View view) {
        onBackPressed();
    }
}